package com.domain.common;

import com.domain.admin.AdminRoute;
import com.domain.blog.BlogRoute;
import com.domain.common.model._MappingKit;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.template.Engine;

/**
 * MyJFinal项目启动配置文件
 * @author Administrator
 *
 */
public class MyJFinalConfig extends JFinalConfig {
	
	public static void main(String[] args) {
		/**
		 * 特别注意：Eclipse 之下建议的启动方式
		 */
		JFinal.start("src/main/webapp", 80, "/", 5);
		
		/**
		 * 特别注意：IDEA 之下建议的启动方式，仅比 eclipse 之下少了最后一个参数
		 */
		// JFinal.start("src/main/webapp", 80, "/");
	}
	
	/**
	 * 配置常量
	 */
	@Override
	public void configConstant(Constants me) {
		PropKit.use("jdbc-mysql-config.properties");
		Prop projectProp = PropKit.use("project-config.properties");
		//设置开发模式
		me.setDevMode(projectProp.getBoolean("devMode", true));
	}

	@Override
	public void configRoute(Routes me) {
		//添加AdminRoute路由
		me.add(new AdminRoute());
		me.add(new BlogRoute());
	}

	@Override
	public void configEngine(Engine me) {
		Prop projectProp = PropKit.use("project-config.properties");
		//添加共享模板
		me.addSharedFunction("/common/_layout.html");
		me.addSharedFunction("/common/_paginate.html");
		me.addSharedFunction("/common/_admin_layout.html");
		me.addSharedFunction("/common/_frontend_layout.html");
		//添加共享变量
		me.addSharedObject("RESOURCE_HOST", projectProp.get("RESOURCE_HOST"));
	}

	@Override
	public void configPlugin(Plugins me) {
		Prop jdbcProp = PropKit.use("jdbc-mysql-config.properties");
		DruidPlugin druidPlugin = new DruidPlugin(jdbcProp.get("jdbc.url"), jdbcProp.get("jdbc.username"), 
				jdbcProp.get("jdbc.password").trim(), jdbcProp.get("jdbc.driverClassName"));
		me.add(druidPlugin);
		
		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		_MappingKit.mapping(arp);
		me.add(arp);
	}
	
	public static DruidPlugin createDruidPlugin() {
		return new DruidPlugin(PropKit.get("jdbc.url"), PropKit.get("jdbc.username"), 
				PropKit.get("jdbc.password").trim(), PropKit.get("jdbc.driverClassName"));
	}

	@Override
	public void configInterceptor(Interceptors me) {
		// TODO Auto-generated method stub

	}

	@Override
	public void configHandler(Handlers me) {
		// TODO Auto-generated method stub

	}

}
